﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace Nirvana
{

    [CustomPropertyDrawer(typeof(AudioImportConfig))]
    public sealed class AudioImportConfigPropertyDrawer : PropertyDrawer
    {
        private int index = 0;
        public override void OnGUI(Rect pos, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(pos, label, property);

           // EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), label);
            EditorGUI.indentLevel++;

            SerializedProperty audioLoadType = property.FindPropertyRelative("audioLoadType");
            SerializedProperty compressionFormat = property.FindPropertyRelative("compressionFormat");
            SerializedProperty sampleRateSetting = property.FindPropertyRelative("sampleRateSetting");
            SerializedProperty sampleRateOverride = property.FindPropertyRelative("sampleRateOverride");
            SerializedProperty quality = property.FindPropertyRelative("quality");
            SerializedProperty forceToMono = property.FindPropertyRelative("forceToMono");
            SerializedProperty loadInBackground = property.FindPropertyRelative("loadInBackground");
            SerializedProperty ambisonic = property.FindPropertyRelative("ambisonic");
            SerializedProperty preloadAudioData = property.FindPropertyRelative("preloadAudioData");


            index = 0;

            DrawAudioLoadTypeGUI(pos, audioLoadType);
            DrawAudioCompressFormatGUI(pos, compressionFormat);
            DrawAudioPreLoadDataGUI(pos, preloadAudioData);
            int offsetY = 0;
            index = index + 2;
            var r1 = new Rect(pos.x, pos.y + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var r2 = new Rect(pos.x, pos.y + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var r3 = new Rect(pos.x, pos.y + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);

            offsetY = 5;
            var r4 = new Rect(pos.x, pos.y+ offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            offsetY = offsetY + 2;
            var r5 = new Rect(pos.x, pos.y + offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            offsetY = offsetY + 5;
            var r6 = new Rect(pos.x, pos.y + offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            offsetY = offsetY + 2;
            var r7 = new Rect(pos.x, pos.y + offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            offsetY = offsetY + 5;
            var r8 = new Rect(pos.x, pos.y + offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            offsetY = offsetY + 2;
            var r9 = new Rect(pos.x, pos.y + offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            offsetY = offsetY + 5;
            var r10 = new Rect(pos.x, pos.y + offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            offsetY = offsetY + 2;
            var r11 = new Rect(pos.x, pos.y + offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);

            EditorGUI.PrefixLabel(r1, new GUIContent("采样率设置，最小化音频的码率有利于节省内存和降低包体大小"));
            EditorGUI.PropertyField(r2, sampleRateSetting);
            EditorGUI.PropertyField(r3, sampleRateOverride);
            EditorGUI.PrefixLabel(r4, new GUIContent("压缩品质(0-1)"));
            EditorGUI.PropertyField(r5, quality);
            EditorGUI.PrefixLabel(r6, new GUIContent("移动平台建议开启，双音道对手机没意义，内存消耗将会减半。适用于那些没有立体效果的音效，比如UI,短音效等。"));
            EditorGUI.PropertyField(r7, forceToMono);
            EditorGUI.LabelField(r8, new GUIContent("加载将在单独的线程上延时进行，不会阻止主线程,响应会延时"));
            EditorGUI.PropertyField(r9, loadInBackground);

            EditorGUI.PrefixLabel(r10, new GUIContent("立体混响声"));
            EditorGUI.PropertyField(r11, ambisonic, new GUIContent("ambisonic"));

            EditorGUI.indentLevel--;

            EditorGUI.EndProperty();

        }

        /// <summary>
        /// 绘制所有属性字段在GUI上所需的整体长度
        /// </summary>
        /// <param name="property"></param>
        /// <param name="label"></param>
        /// <returns></returns>
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            return 45f * EditorGUIUtility.singleLineHeight;
        }

    
        private void DrawAudioLoadTypeGUI(Rect pos,SerializedProperty audioLoadType)
        {
            int startIndex = index;
            var r1 = new Rect(pos.x, pos.y  + ++index * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var r2 = new Rect(pos.x, pos.y +5+ (++index) * EditorGUIUtility.singleLineHeight, pos.width, 7*EditorGUIUtility.singleLineHeight);
            index = index + 7;
            int board = 4;
            var wrap = new Rect(r2.x - board, r2.y - board, pos.width + 2 * board, (index - startIndex) * EditorGUIUtility.singleLineHeight + 2 * board);

            var r4 = new Rect(pos.x, pos.y + (++index) * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);

            EditorGUI.DrawRect(wrap, new Color(0.25f, 0.25f, 0.25f, 1));
            EditorGUI.LabelField(r1, "音频加载方式", GUITheme.LabelTitleStyle);
            EditorGUI.HelpBox(r2,
"DecompressOnLoad：文件加载后立即解压缩保存到内存中(还没播放)，避免了在播放时才解压缩造成的性能消耗。对1秒内的音频使用此选项可以避免动态解压缩的性能开销，因此请勿对大文件使用此选项\n" +
"\nCompressed In Memory: 将声音压缩在内存中并在播放时解压缩\n" +
"\nStreming：动态解码声音。 此方法使用最少量的内存来缓冲从磁盘中逐步读取并在运行中解码的压缩数据。背景音效设置此加载方式\n", MessageType.None);
            GUI.color = Color.yellow;
            EditorGUI.PropertyField(r4,audioLoadType);
            GUI.color = Color.white;
        }

        private void DrawAudioCompressFormatGUI(Rect pos, SerializedProperty compressionFormat)
        {
            index = index +1;
            int startIndex = index;
            var r1 = new Rect(pos.x, pos.y + ++index * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var r2 = new Rect(pos.x, pos.y + 5 + (++index) * EditorGUIUtility.singleLineHeight, pos.width, 7 * EditorGUIUtility.singleLineHeight);
            index = index + 7;
            int board = 4;
            var wrap = new Rect(r2.x - board, r2.y - board, pos.width + 2 * board, (index - startIndex) * EditorGUIUtility.singleLineHeight + 2 * board);

            var r4 = new Rect(pos.x, pos.y + (++index) * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);

            EditorGUI.DrawRect(wrap, new Color(0.25f, 0.25f, 0.25f, 1));
            EditorGUI.LabelField(r1, "音频压缩格式", GUITheme.LabelTitleStyle);
            EditorGUI.HelpBox(r2,
            "PCM：无损压缩，文件占用内存大，对于非常短的声音效果是非常好的.\n" +
            "\nVorbis/MP3: 压缩文件，但与 PCM 音频相比，质量降低。可通过 Quality 滑动条来配置压缩量。此格式最适合中等长度的音效和音乐.\n" +
            "\nADPCM：这种格式对于包含相当多的噪音并且需要大量播放的声音非常有用，比如脚步声、撞击声、武器声。压缩比是PCM的3.5倍，但CPU使用率比MP3/Vorbis格式低得多，这使它成为上述声音类别的首选.\n" +
            "\n在加载时解压缩Vorbis编码的声音将使用大约十倍于内存的内存而对于ADPCM编码约为3.5倍。\n", MessageType.None);
            GUI.color = Color.yellow;
            EditorGUI.PropertyField(r4, compressionFormat);
            GUI.color = Color.white;
        }
        private void DrawAudioPreLoadDataGUI(Rect pos, SerializedProperty preloadAudioData)
        {

            index = index + 1;
            int startIndex = index;
            var r1 = new Rect(pos.x, pos.y + ++index * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var r2 = new Rect(pos.x, pos.y + 5 + (++index) * EditorGUIUtility.singleLineHeight, pos.width, 3 * EditorGUIUtility.singleLineHeight);
            index = index + 3;
            int board = 4;
            var wrap = new Rect(r2.x - board, r2.y - board, pos.width + 2 * board, (index - startIndex) * EditorGUIUtility.singleLineHeight + 2 * board);

            var r4 = new Rect(pos.x, pos.y + (++index) * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);

            EditorGUI.DrawRect(wrap, new Color(0.25f, 0.25f, 0.25f, 1));
            EditorGUI.LabelField(r1, "音频预加载", GUITheme.LabelTitleStyle);
            EditorGUI.HelpBox(r2,
               "预加载，开启此标志，表示音频资源需要在场景加载时提前加载到内存中。一般短音频开启，背景音乐不要开启，会造成游戏场景加载延迟，卡顿。\n", MessageType.None);
            GUI.color = Color.yellow;
            EditorGUI.PropertyField(r4, preloadAudioData);
            GUI.color = Color.white;
        }


    }
}

